<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

import { mapGetters } from 'vuex'
import { logStatistic } from '@/api/monitor/log'
import { stuTeacherIncreased } from '@/api/anniversary/statistics'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '230px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartDataType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      users: { data1: [], data2: [], y_label: ['学生新增数', '教师新增数'], x_data: [] },
      visitis: { data1: [], data2: [], y_label: ['访问量', '登录数'], x_data: [] },
      messages: { data1: [], data2: [], y_label: ['访问量', '登录数'], x_data: [] },
      images: { data1: [], data2: [], y_label: ['访问量', '登录数'], x_data: [] },
      chartData: null,
      chart: null
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  watch: {
    chartDataType: {
      deep: true,
      handler(val) {
        if (val === 'visitis') { this.setOptions(this.visitis) } else if (val === 'users') { this.setOptions(this.users) }
      }
    }
  },
  mounted() {
    var num = 10
    setTimeout(() => {
      var params = {}
      for (var i = 0; i < this.user.roles.length; i++) {
        if (this.user.roles[0].name === '学院管理员') {
          params = { college: this.user.dept.name, num: num }
        } else {
          params = { college: '', num: num }
        }
      }
      stuTeacherIncreased({ num: num }).then(res => {
        if (res != null) {
          this.users.x_data = []
          this.users.data1 = []
          this.users.data2 = []
          for (var i = res.length - 1; i >= 0; i--) {
            this.users.x_data.push(res[i].time)
            this.users.data1.push(res[i].studentCount)
            this.users.data2.push(res[i].teacherCount)
          }
          this.chartData = this.users
        }

        this.$nextTick(() => {
          this.initChart()
        })
      }
      )

      logStatistic(params).then(res => {
        if (res != null) {
          this.visitis.x_data = []
          this.visitis.data1 = []
          this.visitis.data2 = []
          for (var i = res.length - 1; i >= 0; i--) {
            this.visitis.x_data.push(res[i].time)
            this.visitis.data1.push(res[i].count)
            this.visitis.data2.push(res[i].loginCount)
          }
        }
      }
      )
    }, 100)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ data1, data2, x_data, y_label } = {}) {
      this.chart.setOption({
        xAxis: {
          data: x_data,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: [
          {
            type: 'value',
            scale: true,
            name: y_label[0],
            min: 0,
            boundaryGap: [0.2, 0.2]
          },
          {
            type: 'value',
            scale: true,
            name: y_label[1],
            min: 0,
            boundaryGap: [0.2, 0.2]
          }
        ],
        legend: {
          data: y_label
        },
        series: [{
          name: y_label[0], itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              },
              areaStyle: {
                color: '#fdced4'
              }
            }
          },
          smooth: true,
          type: 'line',
          data: data1,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: y_label[1],
          smooth: true,
          type: 'line',
          yAxisIndex: 1,
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#e4e9ef'
              }
            }
          },
          data: data2,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
